package com.springmvc.platform.controller;

import com.springmvc.platform.manage.domain.SysResource;
import com.springmvc.platform.manage.service.SysResourceService;
import com.springmvc.platform.result.ExecuteResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author zhouhong 资源控制层
 * @create 2017/10/8
 * @modify
 */
@Controller
@RequestMapping("resource")
public class ResourceController {
    private static final Logger RESOURCE_LOGGER= LoggerFactory.getLogger(ResourceController.class);
    @Autowired
    private SysResourceService sysResourceService;
    @RequestMapping(value = "findAllResource",method = RequestMethod.POST)
    @ResponseBody
    private Map<Integer,List<SysResource>> findAllResource(){
        return sysResourceService.queryAllResource();
    }

    @RequestMapping(value = "list",method = RequestMethod.GET)
    private String list(ModelMap map){
        map.addAttribute("resourceList",sysResourceService.queryAllTreeTableResource());
        return "resource/list";
    }

    @RequestMapping(value = "add",method = RequestMethod.GET)
    private String toAdd(){
        return "resource/add";
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    private ExecuteResult<SysResource> add(SysResource sysResource){
        ExecuteResult<SysResource> result=new ExecuteResult<SysResource>();
        try {
            sysResourceService.add(sysResource);
            result.setResult(sysResource);
            result.setSuccessMessage("保存成功");
        }catch (Exception e){
            RESOURCE_LOGGER.error("资源保存失败");
            result.addErrorMessage("保存失败");
            return result;
        }
        return result;
    }

    @RequestMapping(value = "tree",method = RequestMethod.GET)
    private String tree(){
        return "resource/tree";
    }

    @RequestMapping(value = "tree",method = RequestMethod.POST)
    @ResponseBody
    private ExecuteResult<List<SysResource>> showResoucesTree(SysResource sysResource){
        ExecuteResult<List<SysResource>> result=new ExecuteResult<List<SysResource>>();
        try {
            result=sysResourceService.queryResourceByResource(sysResource);
        }catch (Exception e){
            RESOURCE_LOGGER.error("资源保存失败",e);
            result.addErrorMessage("保存失败");
            return result;
        }
        return result;
    }

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    private ExecuteResult<SysResource> delete(Long id){
        ExecuteResult<SysResource> result=new ExecuteResult<SysResource>();
        SysResource sysResource=new SysResource();
        try {
            sysResource.setId(id);
            sysResourceService.remove(sysResource);
            result.setResult(sysResource);
            result.setSuccessMessage("删除成功");
        }catch (Exception e){
            RESOURCE_LOGGER.error("删除失败",e);
            result.addErrorMessage("保存失败");
            return result;
        }
        return result;
    }
}
